package com.example.testhooter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.view.View;
import android.view.Window;

public class MainActivity extends Activity implements View.OnClickListener {

	private Button _start,_learn,_upload,_wrtus;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);		
		setContentView(R.layout.activity_main);
		
		_start = (Button)findViewById(R.id.start_btn);
		_learn = (Button)findViewById(R.id.learn_btn);
		_upload = (Button)findViewById(R.id.upload_btn);
		_wrtus = (Button)findViewById(R.id.writeus_btn);
		
		_start.setOnClickListener(this);
		_learn.setOnClickListener(this);
		_upload.setOnClickListener(this);
		_wrtus.setOnClickListener(this);
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.start_btn:
			startbtnCall();				
			break;
		case R.id.learn_btn:
			learnbtnCall();
			break;
		case R.id.upload_btn:
			uploadbtnCall();
			break;
		case R.id.writeus_btn:
			writeusbtnCall();
			break;
		}
	}
	
	private void writeusbtnCall() {
		if(isNetworkAvailable()){
			Log.d("network available"," provide the link");			
		}
		else{
			//show dialog box no internet connection available.
			Log.d("no internet connection","yooooooohiooooooo");
		}
	}
	
	private void uploadbtnCall() {		
		if(isNetworkAvailable()){
			Log.d("network available"," provide the link");			
		}
		else{
			//show dialog box no internet connection available.
			Log.d("no internet connection","yooooooohiooooooo");
		}
	}
	private void startbtnCall() {
		check();		
	}
	
	private void learnbtnCall() {
		if(isNetworkAvailable()){
			Log.d("network available"," provide the link");
			Learn l = new Learn();
			l.readFile();
		}
		else{
			//show dialog box when internet connection not available.
			Log.d("no internet connection","yooooooohiooooooo");
		}
		Log.d("learn_btn", "btn clk");
		
	}
	
	public boolean isNetworkAvailable() {		
	    ConnectivityManager cm = (ConnectivityManager) 
	      getSystemService(Context.CONNECTIVITY_SERVICE);
	    Log.v("chota","bacha");
	    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
	    if (networkInfo != null && networkInfo.isConnected()) {
	        return true;
	    }
	    return false;
	}
	
	private void check() {
		Play p = new Play();
		if(p.isFirstTime(this.getApplicationContext())){
			TestSuite ts = new TestSuite();
			ts.LoadDefaultTestSuite(null, null);
			callCategoryAvtivity(p);
		}
		else{
			callCategoryAvtivity(p);
		}
	}

	private void callCategoryAvtivity(Play p) {
		
		//p.getCategory();
		
		Bundle cat_bundle = new Bundle();
		cat_bundle.putSerializable("cat_data", p);			
		Intent cat_intent = new Intent(getApplicationContext(), CategoryActivity.class);
		cat_intent.putExtras(cat_bundle);
		
        startActivityForResult(cat_intent,0);
		Log.d("","");
	}
	
}